{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7b80af65-0328-4de2-b38d-ec07d765bb57",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-12-18T20:49:11.577043Z",
     "end_time": "2023-12-18T20:49:12.143429Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# 设置最多显示10行\n",
    "pd.set_option('display.max_rows', 10)\n",
    "df = pd.read_excel('待清洗的扑克牌数据集.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "   编号   花色  牌面\n0   1  NaN  大王\n1   2  NaN  小王\n2   3  黑桃♠   A\n3   3  黑桃♠   A\n4   5  黑桃♠  30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>NaN</td>\n      <td>大王</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>NaN</td>\n      <td>小王</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>黑桃♠</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>黑桃♠</td>\n      <td>30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询前5行的数据\n",
    "df.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T20:52:21.016917Z",
     "end_time": "2023-12-18T20:52:21.052686Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号   花色 牌面\n12  13  黑桃♠  J\n14  15  黑桃♠  K\n25  26  红心♥  J\n27  28  红心♥  K\n38  39  梅花♣  J\n40  41  梅花♣  K\n51  52  方块♦  J\n53  54  方块♦  K",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>12</th>\n      <td>13</td>\n      <td>黑桃♠</td>\n      <td>J</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>15</td>\n      <td>黑桃♠</td>\n      <td>K</td>\n    </tr>\n    <tr>\n      <th>25</th>\n      <td>26</td>\n      <td>红心♥</td>\n      <td>J</td>\n    </tr>\n    <tr>\n      <th>27</th>\n      <td>28</td>\n      <td>红心♥</td>\n      <td>K</td>\n    </tr>\n    <tr>\n      <th>38</th>\n      <td>39</td>\n      <td>梅花♣</td>\n      <td>J</td>\n    </tr>\n    <tr>\n      <th>40</th>\n      <td>41</td>\n      <td>梅花♣</td>\n      <td>K</td>\n    </tr>\n    <tr>\n      <th>51</th>\n      <td>52</td>\n      <td>方块♦</td>\n      <td>J</td>\n    </tr>\n    <tr>\n      <th>53</th>\n      <td>54</td>\n      <td>方块♦</td>\n      <td>K</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用正则表达式匹配“牌面”包含j或k的牌\n",
    "import re\n",
    "\n",
    "df[df['牌面'].str.match('[jk]', flags=re.IGNORECASE, na=False)]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T21:00:11.948052Z",
     "end_time": "2023-12-18T21:00:11.963945Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "   编号   花色  牌面\n1   2  NaN  小王\n5   6  黑桃♠   4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>NaN</td>\n      <td>小王</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>6</td>\n      <td>黑桃♠</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询索引在1到5之间，且牌面不是A和30\n",
    "card = ['A', '30']\n",
    "df.query('1 <= index <= 5 and 牌面 not in @card ')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T21:08:45.400800Z",
     "end_time": "2023-12-18T21:08:45.480480Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "    编号   花色 牌面\n28  29  梅花♣  A\n41  42  方块♦  A",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>编号</th>\n      <th>花色</th>\n      <th>牌面</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>28</th>\n      <td>29</td>\n      <td>梅花♣</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>41</th>\n      <td>42</td>\n      <td>方块♦</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询编号大于平均值，且牌面为A\n",
    "df.query('编号 > 编号.mean() and 牌面 == \"A\"')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-12-18T21:10:39.332647Z",
     "end_time": "2023-12-18T21:10:39.402250Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
